#include <vector>

using namespace std;

class Solution {
private:
    inline bool parse(char& val, int& ans) {
        if (val == 'B') {
            return true;
        } else if (val == 'p') {
            ans++;
            return true;
        }
        return false;
    }
public:
    int numRookCaptures(vector<vector<char>>& board) {
        int x, y;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (board[i][j] == 'R') {
                    x = i;
                    y = j;
                    break;
                }
            }
        }
        int ans = 0;
        for (int i = x - 1; i >= 0; i--) {
            if (parse(board[i][y], ans)) {
                break;
            }
        }
        for (int i = x + 1; i < 8; i++) {
            if (parse(board[i][y], ans)) {
                break;
            }
        }
        for (int i = y - 1; i >= 0; i--) {
            if (parse(board[x][i], ans)) {
                break;
            }
        }
        for (int i = y + 1; i < 8; i++) {
            if (parse(board[x][i], ans)) {
                break;
            }
        }
        return ans;
    }
};